﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using StringVariables;

namespace WSManager.Wpf.Dialogs
{
    class VariableDefinitionViewModel : ViewModelBase<VariableDefinition>
    {

        public VariableDefinitionViewModel() : this(new VariableDefinition())
        {
        }

        public VariableDefinitionViewModel(VariableDefinition d)
        {
            this.Item = d;
            Validator = new VariableDefinitionValidator();
        }

        public string Name
        {
            get { return Item.Name; }
            set { Item.Name = value;
                RaisePropertyChanged("Name");
            }
        }

        public bool AllowNull
        {
            get
            {
                return Item.AllowNull;
            }
            set
            {
                Item.AllowNull = value;
                RaisePropertyChanged("AllowNull");
            }
        }

        public string DefaultValue
        {
            get { return Item.DefaultValue; }
            set
            {
                Item.DefaultValue = value;
                RaisePropertyChanged("DefaultValue");
            }
        }

        public VariableType Type
        {
            get { return Item.Type; }
            set
            {
                Item.Type = value;
                RaisePropertyChanged("Type");
                RaisePropertyChanged("DefaultValue");
            }
        }

        public VariableDefinition Source
        {
            get
            {
                return Item;
            }
        }
    }

    /// <summary>
    /// Interaction logic for EditDefinitions.xaml
    /// </summary>
    public partial class EditDefinitions : Window
    {
        private ICollection<VariableDefinition> defSource;
        private Action<List<VariableDefinition>> saveCallback;


        internal ObservableCollection<VariableDefinitionViewModel> Definitions
        {
            get { return (ObservableCollection<VariableDefinitionViewModel>)GetValue(DefinitionsProperty); }
            set { SetValue(DefinitionsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Definitions.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DefinitionsProperty =
            DependencyProperty.Register("Definitions", typeof(ObservableCollection<VariableDefinitionViewModel>), typeof(EditDefinitions), new PropertyMetadata(null));


        public EditDefinitions(ICollection<VariableDefinition> definitions) : this(definitions, null)
        {
        }

        public EditDefinitions(Action<List<VariableDefinition>> saveCallback) : this(null, saveCallback)
        {
        }

        public EditDefinitions(ICollection<VariableDefinition> definitions, Action<List<VariableDefinition>> saveCallback)
        {
            if (definitions == null) definitions = new List<VariableDefinition>();
            if (saveCallback == null) saveCallback = s => { };
            this.defSource = definitions;
            this.saveCallback = saveCallback;
            this.Definitions = new ObservableCollection<VariableDefinitionViewModel>(definitions.Select(d => new VariableDefinitionViewModel(d)));
            this.Definitions.CollectionChanged += Definitions_CollectionChanged;
            InitializeComponent();
            var str = "";
        }

        void Definitions_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
                foreach (var newItem in e.NewItems.Cast<VariableDefinitionViewModel>())
                {
                    defSource.Add(newItem.Source);
                }

            if (e.OldItems != null)
                foreach (var oldItem in e.OldItems.Cast<VariableDefinitionViewModel>())
                {
                    defSource.Remove(oldItem.Source);
                }
           
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Definitions.Add(new VariableDefinitionViewModel(){Name="", DefaultValue="", AllowNull=true});
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            saveCallback(defSource.ToList());
            this.Close();
        }

    }
}
